package com.yunxin.customui.service.builder.echart;

import com.yunxin.customui.bean.echart.*;
import com.yunxin.customui.myinterface.chart.IChartBuilder;
import com.yunxin.tools.commonbean.DataBean;
import com.yunxin.tools.commonbean.exceptions.GeneralException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.yunxin.customui.bean.UIComponent.Custom_ConfigBusinessUtil;

@Service
public class BusinessChartBuilder implements IChartBuilder<ChartBusinessConfig,Map<String, List<DataBean>>> {
    @Override
    public ChartData buildChart(ChartBean chartBean, ChartBusinessConfig config,Map<String,List<DataBean>> dataMap) {


//            if (chartBean.getHandleFunction()!=null){
//                dataMap = chartBean.getHandleFunction().apply(dataMap);
//            }

        //dataMap种是已经补过的数据，不会漏数据
        //此时dataMap中是已经过滤好的数据，且dataMap中有多少key ，曲线图就应该有几条曲线，或柱状图应该有几种柱子、几种图例
        if (dataMap == null || dataMap.isEmpty()){
            return new ChartData(new HashMap<>());
        }
        List<String> strings = new ArrayList<>(dataMap.keySet());

        List<String> xDatas = new ArrayList<>();

        String categoryType = config.getCategoryType();
        if (categoryType.equals("time")){
            //以时间为坐标轴
            Map<String, List<String>> collect = strings.stream().collect(Collectors.groupingBy(key -> key.substring(16, 22)));
            xDatas = dataMap.get(strings.get(0)).stream().map(DataBean::getDataTime).collect(Collectors.toList());
            for (ChartValCfgBean chartValCfgBean : config.getChartValCfgBeans()) {
                String businessId = chartValCfgBean.getDataID();
                List<String> dataIds = collect.get(businessId);
                List<List<String>> dataList = new ArrayList<>();
                for (String dataId : dataIds) {
                    List<String> dataS = dataMap.get(dataId).stream().map(d -> String.valueOf(d.getDataValue())).collect(Collectors.toList());
                    dataList.add(dataS);
                }
                //创建一个线或者一种柱子
                for (List<String> aTypeDataList : dataList) {
                    chartBean.getSeries().add(new Series<>(chartValCfgBean,aTypeDataList));
                }
            }
        }
        else {
            //以id name为坐标轴
            Map<String,List<Map.Entry<String, List<DataBean>>>> businessIdKeyMap = new HashMap<>();
            for (Map.Entry<String, List<DataBean>> entry : dataMap.entrySet()) {
                String businessId = entry.getKey().substring(16, 22);
                List<Map.Entry<String, List<DataBean>>> entryList = businessIdKeyMap.get(businessId);
                if (entryList == null){
                    entryList = new ArrayList<>();
                }
                entryList.add(entry);
                businessIdKeyMap.put(businessId,entryList);
            }
            if (categoryType.equals("business")){

            }else {

                for (ChartValCfgBean chartValCfgBean : config.getChartValCfgBeans()) {
                    xDatas = new ArrayList<>();
                    String businessId = chartValCfgBean.getDataID();
                    List<Map.Entry<String, List<DataBean>>> entries = businessIdKeyMap.get(businessId);
                    if (entries== null){
                        System.out.println("遇到空节点");
                        continue;
                    }
                    List<String> valueList = new ArrayList<>();
                    for (Map.Entry<String, List<DataBean>> entry : entries) {
                        String dataId = entry.getKey();
                        List<DataBean> value = entry.getValue();
                        String name;
                        //这里可以增加一个map将id的描述缓存下来，不用每次调工具
                        if (categoryType.equals("plantname")){
                            name = Custom_ConfigBusinessUtil.get(dataId).getPlantType().getDesc();
                        }else if (categoryType.equals("genname")){
                            name = Custom_ConfigBusinessUtil.get(dataId).getGenType().getDesc();
                        }else if (categoryType.equals("agcname")){
                            name = Custom_ConfigBusinessUtil.get(dataId).getAgcUnion().getDesc();
                        }else if (categoryType.equals("buskvname")){
                            name = Custom_ConfigBusinessUtil.get(dataId).getBusKvType().getDesc();
                        }else if (categoryType.equals("planttype")){
                            name = Custom_ConfigBusinessUtil.get(dataId).getPlantTType().getDesc();
                        }else {
                            throw new GeneralException("创建echart图形时出现异常，categoryType未在程序中定义，categoryType："+categoryType);
                        }
                        String[] split = name.split("\\.");
                        if (split.length>1){
                            name = split[1];
                        }
                        xDatas.add(name);
                        valueList.add(String.valueOf(value.get(0).getDataValue()));
                    }
                    chartBean.getSeries().add(new Series<>(chartValCfgBean,valueList));
                }
            }
        }
        return new ChartData(new HashMap<>());

    }
}
